<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: xajax.categories.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$xajax->registerFunction('getCatTree');
$xajax->registerFunction('getProducts');
$xajax->registerFunction('getProductsInfo');
$xajax->registerFunction('resetInfo');
$xajax->registerFunction('newLinks');
$xajax->registerFunction('catInfos');
$xajax->registerFunction('getMultiForm');
$xajax->registerFunction('actionButtons');
$xajax->registerFunction('setProductsStatus');
$xajax->registerFunction('setFlag');
$xajax->registerFunction('linkCategorie');

function getCatTree($cat_id = '0', $sort_by = 'c.sort_order,cd.categories_name ASC') {
	global $db;
	
	$ajax = new xajaxResponse();
	
	if(empty($_SESSION['customer_id']))
		return $ajax->script("window.location.href = '".FILENAME_LOGIN."';");

	if($cat_id !='0' && (strpos($cat_id, '_') === true)) {
		$cat_id_tmp = explode('_', $cat_id);
		$cat_id_tmp = array_reverse($cat_id_tmp);
		$cat_id = $cat_id_tmp[0];
	}

	$c = '';

	$categories = $db->db_query("SELECT
									c.categories_id,
									cd.categories_name,
									c.parent_id,
									c.sort_order,
									c.categories_status
								FROM
									".TABLE_CATEGORIES." c,
									".TABLE_CATEGORIES_DESCRIPTION." cd
								WHERE
									c.parent_id = '".$cat_id."'
								AND
									c.categories_id = cd.categories_id
								AND
									cd.language_id = '".(int)$_SESSION['languages_id']."'
								ORDER BY
									".$sort_by);


	/**
	 * Breadcrump zusammensetzen
	 */
	$breadcrumb = '<a href="javascript:void(0)" onclick="showOverlay(\'categories_view_left\');showOverlay(\'categories_view_middle\');xajax_getCatTree();">Top</a>';
	if($cat_id != '0') {
		$paths = getParentCat($cat_id);
		foreach($paths AS $path => $value)
			$breadcrumb .= ' » '. $value;
	}
	$ajax->assign('categories_name_left', 'innerHTML', $breadcrumb);

	if($categories->_numOfRows) {
		
		$c  = '	<table width="100%" id="categories_list"><tbody>';
			if($categories->_numOfRows) {
				while(!$categories->EOF) {
					$edit_link = ' onclick="window.location.href=\'categories.php?cPath='.$cat_id.'&menu='.$_GET['menu'].'&cID='.$categories->fields['categories_id'].'&action=edit_category\'"';
					$info_link = ' onclick="xajax_catInfos('.$categories->fields['categories_id'].');"';

					$cs = $categories->fields['categories_status'];

					$categories_status_title = ($cs == 0 ? STATUS_CATEGORIE_ON : STATUS_CATEGORIE_OFF);

					$c .= '	<tr class="li_cat" id="cat_'.$categories->fields['categories_id'].'">
								<td class="sort_grippy">&nbsp;</td>
								<td width="1" valign="middle"><nobr>
									<input class="categories" type="checkbox" value="'.$categories->fields['categories_id'].'" name="multi_categories[]" />
									<span class="status" id="categories_status_id_'.$categories->fields['categories_id'].'">
										<img class="cursor" title="'.$categories_status_title.'" onclick="xajax_setFlag(\'categories\', '.$categories->fields['categories_id'].', '.($cs == 0 ? 1 : 0).')" src="images/icons/status_'.$categories->fields['categories_status'].'.png" alt="" />
									</span></nobr>
								</td>
								<td>
									<a href="javascript:void(0)" onclick="showOverlay(\'categories_view_middle\');xajax_getCatTree('.$categories->fields['categories_id'].');" title="Kategorie '.$categories->fields['categories_name'].' öffnen">
										'.$categories->fields['categories_name'].'
									</a>
								</td>
								<td width="45" valign="middle"><nobr>
									<span class="edit_buttons">
										<img'.$edit_link.' src="images/icons/folder--pencil.png" alt="Kategorie bearbeiten" title="Kategorie '.$categories->fields['categories_name'].' bearbeiten" class="cursor" />
										<img'.$info_link.' src="images/icons/information-frame.png" alt="Kategorie Informationen" title="Informationen für '.$categories->fields['categories_name'].' abrufen" class="cursor" />
									</span></nobr>
								</td>
							</tr>';
					$categories->MoveNext();
				}
				$ajax->assign('count_categories', 'innerHTML', TEXT_CATEGORIES.' '.$categories->_numOfRows);
				$ajax->script('$("#categories").attr("disabled", false);');

			} else {
				$c .= '<tr><td><em>'.TEXT_NO_SUBCAT.'</em></td></tr>';
				$ajax->assign('count_categories', 'innerHTML', TEXT_CATEGORIES.' 0');
				$ajax->script('$("#categories").attr("disabled", true);');
			}
		$c .= '	</tbody></table>';

	} else {
		if($cat_id > 0)
		 	$c .= '<table width="100%" id="categories_list"><tbody><tr><td><em>'.TEXT_NO_MORE_SUBCAT.'</em></td></tr></tbody></table>';
		else
		 	$c .= '<table width="100%" id="categories_list"><tbody><tr><td><em>'.TEXT_NO_SUBCAT.'</em></td></tr></tbody></table>';

		$ajax->assign('count_categories', 'innerHTML', TEXT_CATEGORIES.' 0');
		$ajax->script('$("#categories").attr("disabled", true);');
	}

	$ajax->assign('categories_view_left', 'innerHTML', $c);
	$ajax->assign('cPath', 'value', $cat_id);
	$ajax->call('xajax_getProducts', $cat_id);

	$ajax->call('xajax_newLinks', $cat_id);

	$ajax->call('xajax_actionButtons', $cat_id);

	$ajax->call('xajax_resetInfo');

	return $ajax;
}

function getParentCat($cat_id) {
	global $db;
	
	if(!is_array($back_link));
		$back_link = array();

	$name = $db->db_query("SELECT
								cd.categories_name,
								c.parent_id
							FROM
								".TABLE_CATEGORIES." c,
								".TABLE_CATEGORIES_DESCRIPTION." cd
							WHERE
								cd.categories_id = '".$cat_id."'
							AND
								c.categories_id = '".$cat_id."'
							AND
								cd.language_id = '".(int)$_SESSION['languages_id']."'");

	if($name->_numOfRows) {
		$tmp_link[] = '<a href="javascript:void(0)" onclick="xajax_getCatTree('.$cat_id.');"><b>'.$name->fields['categories_name'].'</b></a>';
		$back_link = array_merge($tmp_link, $back_link);
	}
	if($name->fields['parent_id'] != '0')
		$back_link = array_merge(getParentCat($name->fields['parent_id']), $back_link);

	return $back_link;
}

function getProducts($cat_id = '0', $sort_by = 'p.products_sort, pd.products_name ASC') {
	global $db;
	
	$ajax = new xajaxResponse();
	
	if(empty($_SESSION['customer_id']))
		return $ajax->script("window.location.href = '".FILENAME_LOGIN."';");

	$products = $db->db_query("SELECT
									p.products_tax_class_id,
									p.products_sort,
									p.products_id,
									p.products_model,
									pd.products_name,
									pd.products_url_alias,
									p.products_quantity,
									p.products_image,
									p.products_price,
									p.products_discount_allowed,
									p.products_date_added,
									p.products_last_modified,
									p.products_date_available,
									p.products_status,
									p.products_startpage,
									p.products_startpage_sort
								FROM
									".TABLE_PRODUCTS." p,
									".TABLE_PRODUCTS_DESCRIPTION." pd,
									".TABLE_PRODUCTS_TO_CATEGORIES." p2c
								WHERE
									p.products_id = pd.products_id
								AND
									pd.language_id = '".(int)$_SESSION['languages_id']."'
								AND
									p.products_id = p2c.products_id
								AND
									p2c.categories_id = '".(int)$cat_id."'
								ORDER BY
									".$sort_by);
	
	$p 	= '	<table width="100%" id="products_list"><tbody>';
		if($products->_numOfRows) {
			while(!$products->EOF) {
				$edit_link = ' onclick="window.location.href=\'categories.php?cPath='.$cat_id.'&menu='.$_GET['menu'].'&pID='.$products->fields['products_id'].'&action=new_product\'"';
				$info_link = ' onclick="xajax_getProductsInfo('.$products->fields['products_id'].');"';

				$ps = $products->fields['products_status'];

				$products_status_title = ($ps == 0 ? STATUS_PRODUCTS_ON : STATUS_PRODUCTS_OFF);

				$p .= '	<tr class="li_cat p_sort" id="p_'.$products->fields['products_id'].'">
							<td class="sort_grippy" valign="middle">&nbsp;</td>
							<td width="1" valign="middle"><nobr>
								<input class="products" type="checkbox" value="'.$products->fields['products_id'].'" name="multi_products[]" />
								<span class="status" id="products_status_id_'.$products->fields['products_id'].'">
									<img class="cursor" title="'.$products_status_title.'" onclick="xajax_setFlag(\'products\', '.$products->fields['products_id'].', '.($ps == 0 ? 1 : 0).')" src="images/icons/status_'.$products->fields['products_status'].'.png" alt="" />
								</span>
								</nobr>
							</td>
							<td>
								'.$products->fields['products_name'].'
							</td>
							<td width="45" valign="middle"><nobr>
								<span class="edit_buttons">
							    	<img'.$edit_link.' src="images/icons/folder--pencil.png" alt="Produkt bearbeiten" title="Produkt '.$products->fields['products_name'].' bearbeiten" class="cursor" />
									<img'.$info_link.' src="images/icons/information-frame.png" alt="Produkt Informationen" title="Informationen für '.$products->fields['products_name'].' abrufen" class="cursor" />
								</span></nobr>
							</td>';
				$p .= '</tr>';
				$products->MoveNext();
			}
			$ajax->assign('count_products', 'innerHTML', TEXT_PRODUCTS.' '.$products->_numOfRows);
			$ajax->script('$("#products").attr("disabled", false);');
			$ajax->script('$("#products_list").sortable({axis:"y"});');

		} else {
		 	$p .= '<tr><td><em>keine Produkte vorhanden</em></td></tr>';
		 	$ajax->assign('count_products', 'innerHTML', TEXT_PRODUCTS.' 0');
			$ajax->script('$("#products").attr("disabled", true);');
		}
	$p .= '	</tbody></table>';

	$ajax->assign('categories_view_middle', 'innerHTML', $p);

	return $ajax;
}

function getProductsInfo($pid) {
	global $db;
	
	$ajax = new xajaxResponse();
	
	if(empty($_SESSION['customer_id']))
		return $ajax->script("window.location.href = '".FILENAME_LOGIN."';");
	
	$products = $db->db_query("SELECT
									p.products_tax_class_id,
									p.products_sort,
									p.products_id,
									p.products_model,
									pd.products_name,
									pd.products_url_alias,
									pd.products_viewed,
									p.products_quantity,
									p.products_image,
									p.products_price,
									p.products_ordered,
									p.products_discount_allowed,
									p.products_date_added,
									p.products_tax_class_id,
									p.products_last_modified,
									p.products_date_available,
									p.products_status,
									p.products_startpage,
									p.products_startpage_sort
								FROM
									".TABLE_PRODUCTS." p,
									".TABLE_PRODUCTS_DESCRIPTION." pd
								WHERE
									p.products_id = pd.products_id
								AND
									pd.language_id = '".(int)$_SESSION['languages_id']."'
								AND
									p.products_id = '".$pid."'");

	require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.price.php');
	require_once(DIR_FS_INC.'inc.get_tax_description.php');

	$ajax_price = new price($_SESSION['currency'], $_SESSION['customers_status']['customers_status_id'], $db);

	$p  = '	<table width="100%">';
	$p .= '		<tr>';
	$p .= '			<td colspan="2" align="center">
						<a href="javascript:void(0)" class="button" onclick="crossselling('.$products->fields['products_id'].');">'.BUTTON_EDIT_CROSS_SELLING.'</a></td>';
	$p .= '		</tr>';
	$p .= '		<tr>';
	$p .= '			<td colspan="2"><h2>'.$products->fields['products_name'].'</h2></td>';
	$p .= '		</tr>';
	if(!empty($products->fields['products_url_alias'])) {
	$p .= '		<tr>';
	$p .= '			<td>URL-Alias</td>';
	$p .= '			<td>'.$products->fields['products_url_alias'].'.html</td>';
	$p .= '		</tr>';
	}
	$p .= '		<tr>';
	$p .= '			<td width="30%">Bild:</td>';
	$p .= '			<td>';
					if(!empty($products->fields['products_image']))
	$p .= '				<img src="'.DIR_WS_CATALOG_THUMBNAIL_IMAGES.$products->fields['products_image'].'" alt="'.$products->fields['products_name'].'" /></td>';
					else
	$p .= '				<em>'.TEXT_IMAGE_NONEXISTENT.'</em>';
	$p .= '		</tr>';
	$p .= '		<tr>';
	$p .= '			<td>Artikel-Nr.:</td>';
	$p .= '			<td>'.(!empty($products->fields['products_model'])?$products->fields['products_model']:'-').'</td>';
	$p .= '		</tr>';
	$p .= '		<tr>';
	$p .= '			<td>'.TEXT_DATE_ADDED.'</td>';
	$p .= '			<td>'.date_short($products->fields['products_date_added']).'</td>';
	$p .= '		</tr>';
	$p .= '		<tr>';
	$p .= '			<td>'.TEXT_LAST_MODIFIED.'</td>';
	$p .= '			<td>'.date_short($products->fields['products_last_modified']).'</td>';
	$p .= '		</tr>';
	$p .= '		<tr>';
	$p .= '			<td>'.TEXT_DATE_AVAILABLE.'</td>';
	$p .= '			<td>'.(date_short($products->fields['products_date_available'])?date_short($products->fields['products_date_available']):'<em>sofort</em>').'</td>';
	$p .= '		</tr>';
	$p .= '		<tr>';
	$p .= '			<td>'.TEXT_PRODUCTS_PRICE_INFO.'</td>';
	$p .= '			<td>'.$ajax_price->GetPrice($products->fields['products_id'], true, 1, $products->fields['products_tax_class_id'], $products->fields['products_price']).'</td>';
	$p .= '		</tr>';
	$p .= '		<tr>';
	$p .= '			<td>Steuersatz:</td>';
	$p .= '			<td>'.get_tax_description($products->fields['products_tax_class_id']).'</td>';
	$p .= '		</tr>';
	$p .= '		<tr><td colspan="2">&nbsp;</td></tr>';
	$p .= '		<tr>';
	$p .= '			<td>Angesehen:</td>';
	$p .= '			<td>'.$products->fields['products_viewed'].'x</td>';
	$p .= '		</tr>';
	$p .= '		<tr>';
	$p .= '			<td>Verkauft:</td>';
	$p .= '			<td>'.($products->fields['products_ordered']!=0 ? $products->fields['products_ordered'] : '<em>bisher noch nicht</em>').'</td>';
	$p .= '		</tr>';
	$p .= '	</table>';

	$ajax->assign('categories_view_right', 'innerHTML', $p);

	return $ajax;
}

function catInfos($cat_id) {
	global $db;
	$ajax = new xajaxResponse();
	
	if(empty($_SESSION['customer_id']))
		return $ajax->script("window.location.href = '".FILENAME_LOGIN."';");
	
	$categories = $db->db_query("SELECT
									c.categories_id,
									cd.categories_name,
									c.categories_image,
									c.categories_template,
									c.parent_id,
									c.sort_order,
									c.date_added,
									c.last_modified,
									c.categories_status,
									cd.categories_url_alias
								FROM
									".TABLE_CATEGORIES." c,
									".TABLE_CATEGORIES_DESCRIPTION." cd
								WHERE
									c.categories_id = '".$cat_id."'
								AND
									c.categories_id = '".$cat_id."'
								AND
									cd.language_id = '".(int)$_SESSION['languages_id']."'");

	$c  = '	<table width="100%">';
	$c .= '		<tr>';
	$c .= '			<td colspan="2"><h2>'.$categories->fields['categories_name'].'</h2></td>';
	$c .= '		</tr>';
	$c .= '		<tr>';
	$c .= '			<td width="30%">Bild:</td>';
	$c .= '			<td>'.info_image_c($categories->fields['categories_image'], $categories->fields['categories_name']).'</td>';
	$c .= '		</tr>';
	$c .= '		<tr>';
	$c .= '			<td>'.TEXT_DATE_ADDED.'</td>';
	$c .= '			<td>'.date_short($categories->fields['date_added']).'</td>';
	$c .= '		</tr>';
	$c .= '		<tr>';
	$c .= '			<td>'.TEXT_LAST_MODIFIED.'</td>';
	$c .= '			<td>'.date_short($categories->fields['last_modified']).'</td>';
	$c .= '		</tr>';
	$c .= '		<tr>';
	$c .= '			<td>Kategorie Template:</td>';
	$c .= '			<td>'.$categories->fields['categories_template'].'</td>';
	$c .= '		</tr>';
	$c .= '	</table>';

	$ajax->assign('categories_view_right', 'innerHTML', $c);

	return $ajax;
}

function newLinks($cat_id) {
	$ajax = new xajaxResponse();

	$newLinks = '<a class="button" href="'.href_link('categories.php', 'cPath='.$cat_id.'&action=new_category').'"><span class="button_plus-black" style="background-image:url(images/icons/plus-black.png)">'.BUTTON_NEW_CATEGORIES.'</span></a><a class="button" href="'.href_link(FILENAME_CATEGORIES, 'cPath='.$cat_id.'&action=new_product').'"><span class="button_plus-black" style="background-image:url(images/icons/plus-black.png)">'.BUTTON_NEW_PRODUCTS.'</span></a>';

	$ajax->assign('categories_name_right', 'innerHTML', $newLinks);

	return $ajax;
}

function resetInfo() {
	$ajax = new xajaxResponse();

	$ajax->assign('categories_view_right', 'innerHTML', '');

	return $ajax;
}

function actionButtons($cat_id = '', $pID = '') {
	$ajax = new xajaxResponse();

	$button = '<a href="javascript:void(0)" class="button" onclick="collect_ids('.$cat_id.',\'copy\');"><span class="button_document-copy-black" style="background-image:url(images/icons/document-copy-black.png)">'.BUTTON_COPY.'</span></a>
				<a href="javascript:void(0)" class="button" onclick="collect_ids('.$cat_id.',\'move\');"><span class="button_arrow-move-black" style="background-image:url(images/icons/arrow-move-black.png)">'.BUTTON_MOVE.'</span></a>
				<a href="javascript:void(0)" class="button" onclick="collect_ids('.$cat_id.',\'delete\');"><span class="button_cross-black" style="background-image:url(images/icons/cross-black.png)">'.BUTTON_DELETE.'</span></a>
				<a href="javascript:void(0)" class="button" onclick="collect_ids('.$cat_id.',\'status_on\');">'.BUTTON_STATUS_ON.'</a>
				<a href="javascript:void(0)" class="button" onclick="collect_ids('.$cat_id.',\'status_off\');">'.BUTTON_STATUS_OFF.'</a>';

	$ajax->assign('action_buttons', 'innerHTML', $button);

	return $ajax;
}

function getMultiForm($action, $cat_ids, $pro_ids, $cPath) {
	
	require('includes/xajax/xajax_helper/categories.helper.php');
	$helper = new categoriesHelper;

	switch ($action) {
		case 'copy_to';
			$title = 'Kopieren';
			$form = $helper->multi_copy_to($cat_ids, $pro_ids, $cPath);
			break;
		case 'move_to';
			$title = 'Verschieben';
			$form = $helper->multi_move_to($cat_ids, $pro_ids, $cPath);
			break;
		case 'delete';
			$title = 'Löschen';
			$form = $helper->multi_delete($cat_ids, $pro_ids, $cPath);
			break;
	}
	$ajax = new xajaxResponse();
	
	if(empty($_SESSION['customer_id']))
		return $ajax->script("window.location.href = '".FILENAME_LOGIN."';");
	
	$ajax->assign('dialog', 'innerHTML', $form);
	$dialog = '$("#dialog").dialog({close: function(){xajax_emptyDiv(\'dialog\', true)}, modal: true,title: \''.$title.'\',buttons: {"'.$title.'":function(){$("#dialog_form").submit();},"'.BUTTON_CANCEL.'": function() {$(this).dialog("close");}},width: 860,height:600,open: function(event,ui) {$(\'.ui-dialog-content\').css({backgroundImage:\'none\'});}});$(\'.ui-dialog\').css({position:"fixed"});setTimeout(\'$("#show_form").dialog("open")\', 150);';
	$ajax->script($dialog);
	return $ajax;
}

function setFlag($what = 'categories', $id, $flag = 0) {
	global $db;
	
	$ajax = new xajaxResponse();
	
	if(empty($_SESSION['customer_id']))
		return $ajax->script("window.location.href = '".FILENAME_LOGIN."';");
	
	$db->db_query("UPDATE ".DB_PREFIX.$what." SET ".$what."_status = '".$flag."' WHERE ".$what."_id = '".$id."' ");

	if($what == 'categories')
		$products_status_title = ($flag == 0 ? STATUS_CATEGORIE_ON : STATUS_CATEGORIE_OFF);
	else
		$products_status_title = ($flag == 0 ? STATUS_PRODUCTS_ON : STATUS_PRODUCTS_OFF);

	$ajax->assign($what.'_status_id_'.$id, 'innerHTML', '<img class="cursor" title="'.$products_status_title.'" onclick="xajax_setFlag(\''.$what.'\', '.$id.', '.($flag == 0 ? 1 : 0).')" src="images/icons/status_'.$flag.'.png" alt="" />');
	return $ajax;
}